package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;
import net.carefx.framework.util.ConvertUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.DeptVsDispensary;
import com.th.supcom.pds.impl.manager.DeptVsDispensaryManager;
import com.th.supcom.pds.internal.api.phamapply.DeptVsDispensaryBasicService;

public class DeptVsDispensaryBasicServiceImpl implements DeptVsDispensaryBasicService
{
    private DeptVsDispensaryManager m_deptVsDispensaryManager;
    private TranslatorManager m_translatorManager;

    public void setDeptVsDispensaryManager (DeptVsDispensaryManager deptVsDispensaryManager)
    {
        m_deptVsDispensaryManager = deptVsDispensaryManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    @Override
    public DeptVsDispensary getDeptVsDispensaryByID (Long id)
    {
        s_logger.info ("调用getDeptVsDispensaryByID参数 ：id:{}", new Object[]
        { id });
        if (null == id)
        {
            s_logger.error ("通过主键查询科室与药房对照关系表时，主键不能为空");
            throw new IllegalArgumentException ("通过主键查询科室与药房对照关系表时，主键不能为空");
        }
        try
        {
            DeptVsDispensary deptVsDispensary = m_deptVsDispensaryManager.getDeptVsDispensaryByID (id);
            deptVsDispensary = m_translatorManager.translate (deptVsDispensary);
            return deptVsDispensary;
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键查询科室与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键查询科室与药房对照关系表异常", e);
        }
    }

    @Override
    public List <DeptVsDispensary> findDeptVsDispensaryListByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用findDeptVsDispensaryListByExample参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("通过条件查询科室与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("通过条件查询科室与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            List <DeptVsDispensary> list = m_deptVsDispensaryManager.findDeptVsDispensaryListByExample (deptVsDispensary);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("通过条件查询科室与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过条件查询科室与药房对照关系表异常", e);
        }
    }

    @Override
    public List <DeptVsDispensary> findDeptVsDispensaryListByNurseCode (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用findDeptVsDispensaryListByNurseCode参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("根据护理单元查询其对应发药药房时，条件对象不能为空");
            throw new IllegalArgumentException ("根据护理单元查询其对应发药药房时，条件对象不能为空");
        }
        try
        {
            List <DeptVsDispensary> list = m_deptVsDispensaryManager.findDeptVsDispensaryListByNurseCode (deptVsDispensary);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("根据护理单元查询其对应发药药房异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("根据护理单元查询其对应发药药房异常", e);
        }
    }

    @Override
    public int findDeptVsDispensaryCountByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用findDeptVsDispensaryCountByExample参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("通过条件查询科室与药房对照关系表记录数时，条件对象不能为空");
            throw new IllegalArgumentException ("通过条件查询科室与药房对照关系表记录数时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.findDeptVsDispensaryCountByExample (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("通过条件查询科室与药房对照关系表记录数异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过条件查询科室与药房对照关系表记录数异常", e);
        }
    }

    @Override
    public int modifyDeptVsDispensary (DeptVsDispensary deptVsDispensary)
    {
        sortWhenEdit (deptVsDispensary);

        s_logger.info ("调用modifyDeptVsDispensary参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("更新科室与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新科室与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("更新科室与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新科室与药房对照关系表异常", e);
        }
    }

    @Override
    public int modifyDeptVsDispensaryByExample (DeptVsDispensary deptVsDispensary)
    {
        sortWhenEdit (deptVsDispensary);

        s_logger.info ("调用modifyDeptVsDispensaryByExample参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("更新科室与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新科室与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensaryByExample (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("更新科室与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新科室与药房对照关系表异常", e);
        }
    }

    @Override
    public void addDeptVsDispensary (DeptVsDispensary deptVsDispensary)
    {
        existSameConfig (deptVsDispensary);
        sortWhenAdd (deptVsDispensary);

        s_logger.info ("调用addDeptVsDispensary参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("添加科室与药房对照关系时，条件对象不能为空");
            throw new IllegalArgumentException ("添加科室与药房对照关系时，条件对象不能为空");
        }
        try
        {
            m_deptVsDispensaryManager.addDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("添加科室与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("添加科室与药房对照关系异常", e);
        }
    }

    /**
     * 判断是否有相同的配置
     */
    private void existSameConfig (DeptVsDispensary deptVsDispensary)
    {
        // 如果科室、药房、班次有相同则提示已存在相同配置
        DeptVsDispensary oldDeptVsDispensary = ConvertUtils.convert (DeptVsDispensary.class, deptVsDispensary);
        oldDeptVsDispensary.setPrioriyt (null);

        List <DeptVsDispensary> deptVsDispensaries = findDeptVsDispensaryListByExample (oldDeptVsDispensary);
        if (!deptVsDispensaries.isEmpty ())
        {
            throw new BusinessException ("该配置已存在！");
        }
    }

    /**
     * 进行重新排序
     */
    private void sortWhenAdd (DeptVsDispensary deptVsDispensary)
    {
        // 如果科室、药房、班次有相同则提示已存在相同配置
        DeptVsDispensary oldDeptVsDispensary = ConvertUtils.convert (DeptVsDispensary.class, deptVsDispensary);
        oldDeptVsDispensary.setPrioriyt (null);
        oldDeptVsDispensary.setDispensary (null);

        List <DeptVsDispensary> deptVsDispensaries = findDeptVsDispensaryListByExample (oldDeptVsDispensary);
        if (deptVsDispensaries.isEmpty ())
        {
            return;
        }

        // 找到该配置关系位置
        int index = 0;

        for (int i = 0; i < deptVsDispensaries.size (); i++)
        {
            if (deptVsDispensary.getPrioriyt ().equals (deptVsDispensaries.get (i).getPrioriyt ()))
            {
                index = i;
                break;
            }
        }

        if (index == 0)
        {
            return;
        }

        // 将后面的配置关系的优先级递增1
        for (int i = index; i < deptVsDispensaries.size (); i++)
        {
            deptVsDispensaries.get (i).setPrioriyt (deptVsDispensaries.get (i).getPrioriyt () + 1);
        }

        modifyDeptVsDispensaryList (deptVsDispensaries);
    }

    /**
     * 编辑的时候对优先级进行重排
     */
    private void sortWhenEdit (DeptVsDispensary deptVsDispensary)
    {
        // 如果科室、药房、班次有相同则提示已存在相同配置
        DeptVsDispensary oldDeptVsDispensary = ConvertUtils.convert (DeptVsDispensary.class, deptVsDispensary);
        oldDeptVsDispensary.setPrioriyt (null);
        oldDeptVsDispensary.setDispensary (null);
        oldDeptVsDispensary.setDeptVsDispensaryId (null);

        List <DeptVsDispensary> deptVsDispensaries = findDeptVsDispensaryListByExample (oldDeptVsDispensary);
        if (deptVsDispensaries.isEmpty ())
        {
            return;
        }

        // 找到该配置编辑前的位置
        int index = 0;

        for (int i = 0; i < deptVsDispensaries.size (); i++)
        {
            if (deptVsDispensaryEquals (deptVsDispensaries.get (i), deptVsDispensary))
                index = i;
            {
                break;
            }
        }

        // 如果编辑前的优先级比编辑后的大
        if (index > deptVsDispensary.getPrioriyt () - 1)
        {
            for (int i = deptVsDispensary.getPrioriyt () - 1; i < index; i++)
            {
                deptVsDispensaries.get (i).setPrioriyt (deptVsDispensaries.get (i).getPrioriyt () + 1);
            }
        }

        // 如果编辑前的优先级比编辑后的小
        if (index < deptVsDispensary.getPrioriyt () - 1)
        {
            for (int i = deptVsDispensary.getPrioriyt () - 1; i > index; i--)
            {
                deptVsDispensaries.get (i).setPrioriyt (deptVsDispensaries.get (i).getPrioriyt () - 1);
            }
        }

        modifyDeptVsDispensaryList (deptVsDispensaries);
    }

    /**
     * 判断两个配置关系是否相同
     * 
     * @return
     */
    private boolean deptVsDispensaryEquals (DeptVsDispensary source, DeptVsDispensary target)
    {
        if (!source.getDeptCode ().equals (target.getDeptCode ()))
        {
            return false;
        }

        if (!source.getShift ().equals (target.getShift ()))
        {
            return false;
        }

        return true;
    }

    /**
     * 删除时对优先级进行重排
     * 
     * @param deptVsDispensary
     */
    private void sortWhenDelete (DeptVsDispensary deptVsDispensary)
    {
        // 如果科室、药房、班次有相同则提示已存在相同配置
        DeptVsDispensary oldDeptVsDispensary = ConvertUtils.convert (DeptVsDispensary.class, deptVsDispensary);
        oldDeptVsDispensary.setPrioriyt (null);
        oldDeptVsDispensary.setDispensary (null);
        oldDeptVsDispensary.setDeptVsDispensaryId (null);

        List <DeptVsDispensary> deptVsDispensaries = findDeptVsDispensaryListByExample (oldDeptVsDispensary);
        if (deptVsDispensaries.isEmpty ())
        {
            return;
        }

        // 找到该配置编辑前的位置
        int index = 0;

        for (int i = 0; i < deptVsDispensaries.size (); i++)
        {
            if (deptVsDispensaryEquals (deptVsDispensaries.get (i), deptVsDispensary))
            {
                index = i;
                break;
            }
        }

        for (int i = deptVsDispensaries.size () - 1; i > index; i--)
        {
            deptVsDispensaries.get (i).setPrioriyt (deptVsDispensaries.get (i).getPrioriyt () - 1);
        }

        modifyDeptVsDispensaryList (deptVsDispensaries);
    }

    @Override
    public int removeDeptVsDispensaryByID (Long id)
    {
        s_logger.info ("调用removeDeptVsDispensaryByID参数 ：id:{}", new Object[]
        { id });
        if (null == id)
        {
            s_logger.error ("通过主键删除科室与药房对照关系时，主键不能为空");
            throw new IllegalArgumentException ("通过主键删除科室与药房对照关系时，主键不能为空");
        }

        DeptVsDispensary deptVsDispensary = getDeptVsDispensaryByID (id);
        if (null == deptVsDispensary)
        {
            s_logger.error ("药房科室配置关系不存在");
            throw new BusinessException ("药房科室配置关系不存在");
        }

        sortWhenDelete (deptVsDispensary);

        try
        {
            return m_deptVsDispensaryManager.removeDeptVsDispensaryByID (id);
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除科室与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键删除科室与药房对照关系异常", e);
        }
    }

    @Override
    public int modifyDeptVsDispensaryList (List <DeptVsDispensary> deptVsDispensaryList)
    {
        s_logger.info ("调用modifyDeptVsDispensaryList参数 ：deptVsDispensaryList:{}", new Object[]
        { deptVsDispensaryList });
        if (null == deptVsDispensaryList)
        {
            s_logger.error ("批量更新科室与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("批量更新科室与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensaryList (deptVsDispensaryList);
        }
        catch (Exception e)
        {
            s_logger.error ("批量更新科室与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("批量更新科室与药房对照关系表异常", e);
        }
    }

    @Override
    public List <DeptVsDispensary> findDispenseTypeListByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用findDispenseTypeListByExample参数 ：DeptVsDispensary deptVsDispensary={}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("查询摆药类型与药房对照关系表时，对象不能为空");
            throw new IllegalArgumentException ("查询摆药类型与药房对照关系表时，对象不能为空");
        }
        try
        {
            List <DeptVsDispensary> deptVsDispensaries = m_deptVsDispensaryManager.findDispenseTypeListByExample (deptVsDispensary);
            return m_translatorManager.translate (deptVsDispensaries);
        }
        catch (Exception e)
        {
            s_logger.error ("查询摆药摆药类型与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询摆药类型与药房对照关系表异常", e);
        }
    }

    @Override
    public List <DeptVsDispensary> findPhamMaterialTypeListByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用findPhamMaterialTypeListByExample参数 ：DeptVsDispensary deptVsDispensary={}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("查询药品材质与药房对照关系表时，对象不能为空");
            throw new IllegalArgumentException ("查询药品材质与药房对照关系表时，对象不能为空");
        }
        try
        {
            List <DeptVsDispensary> deptVsDispensaries = m_deptVsDispensaryManager.findPhamMaterialTypeListByExample (deptVsDispensary);
            return m_translatorManager.translate (deptVsDispensaries);
        }
        catch (Exception e)
        {
            s_logger.error ("查询药品材质类型与药房对照关系表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药品材质与药房对照关系表异常", e);
        }
    }

    @Override
    public int modifyDispenseTypeByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用modifyDispenseTypeByExample参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("更新药品类型与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新药品类型与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药品类型与药房对照关系表时异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药品类型与药房对照关系表时异常", e);
        }
    }

    @Override
    public void addDispenseType (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用addDispenseType参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("添加药品类型与药房对照关系时，条件对象不能为空");
            throw new IllegalArgumentException ("添加药品类型与药房对照关系时，条件对象不能为空");
        }
        try
        {
            m_deptVsDispensaryManager.addDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("添加药品类型与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("添加药品类型与药房对照关系异常", e);
        }

    }

    @Override
    public int removeDispenseTypeByID (Long id)
    {
        s_logger.info ("调用removeDispenseTypeByID参数 ：id:{}", new Object[]
        { id });
        if (null == id)
        {
            s_logger.error ("通过主键删除药品类型与药房对照关系时，主键不能为空");
            throw new IllegalArgumentException ("通过主键删除药品类型与药房对照关系时，主键不能为空");
        }

        DeptVsDispensary deptVsDispensary = getDeptVsDispensaryByID (id);
        if (null == deptVsDispensary)
        {
            s_logger.error ("药品类型药房配置关系不存在");
            throw new BusinessException ("药品类型药房配置关系不存在");
        }

        try
        {
            return m_deptVsDispensaryManager.removeDeptVsDispensaryByID (id);
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除药品类型与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键删除药品类型与药房对照关系异常", e);
        }
    }

    @Override
    public int modifyPhamMeterialTypeByExample (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用modifyPhamMeterialTypeByExample参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("更新药品材质与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新药品材质与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药品材质与药房对照关系表时异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药品材质与药房对照关系表时异常", e);
        }
    }

    @Override
    public void addPhamMeterialType (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用addPhamMeterialType参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("添加药品材质类型与药房对照关系时，条件对象不能为空");
            throw new IllegalArgumentException ("添加药品材质类型与药房对照关系时，条件对象不能为空");
        }
        try
        {
            m_deptVsDispensaryManager.addDeptVsDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("添加药品材质类型与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("添加药品材质类型与药房对照关系异常", e);
        }

    }

    @Override
    public int removePhamMeterialTypeByID (Long id)
    {

        s_logger.info ("调用removePhamMeterialTypeByID参数 ：id:{}", new Object[]
        { id });
        if (null == id)
        {
            s_logger.error ("通过主键删除药品材质与药房对照关系时，主键不能为空");
            throw new IllegalArgumentException ("通过主键删除药品材质与药房对照关系时，主键不能为空");
        }

        DeptVsDispensary deptVsDispensary = getDeptVsDispensaryByID (id);
        if (null == deptVsDispensary)
        {
            s_logger.error ("药品材质与药房配置关系不存在");
            throw new BusinessException ("药品材质与药房配置关系不存在");
        }

        try
        {
            return m_deptVsDispensaryManager.removeDeptVsDispensaryByID (id);
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除药品材质与药房对照关系异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键删除药品材质与药房对照关系异常", e);
        }
    }

    @Override
    public int modifyDeptVsDispensaryByDispensary (DeptVsDispensary deptVsDispensary)
    {
        s_logger.info ("调用modifyDeptVsDispensaryByDispensary参数 ：deptVsDispensary:{}", new Object[]
        { deptVsDispensary });
        if (null == deptVsDispensary)
        {
            s_logger.error ("更新药品材质与药房对照关系表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新药品材质与药房对照关系表时，条件对象不能为空");
        }
        try
        {
            return m_deptVsDispensaryManager.modifyDeptVsDispensaryByDispensary (deptVsDispensary);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药品材质与药房对照关系表时异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药品材质与药房对照关系表时异常", e);
        }
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (DeptVsDispensaryBasicServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
